package com.LittleHan.crm.workbench.controller;

import com.LittleHan.crm.exception.ActivityCreateException;
import com.LittleHan.crm.exception.ActivityDeleteException;
import com.LittleHan.crm.exception.ActivityRemarkException;
import com.LittleHan.crm.exception.ActivityUpdateException;
import com.LittleHan.crm.settings.entity.User;
import com.LittleHan.crm.settings.service.UserService;
import com.LittleHan.crm.utils.DateTimeUtil;
import com.LittleHan.crm.utils.UUIDUtil;
import com.LittleHan.crm.vo.PaginationVo;
import com.LittleHan.crm.workbench.entity.Activity;
import com.LittleHan.crm.workbench.entity.ActivityRemark;
import com.LittleHan.crm.workbench.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller(value = "workbenchController")
@RequestMapping(value = "/workbenchCon")
public class WorkbenchController {
    private static final String EDIT_FLAG_YES="1";
    private static final String EDIT_FLAG_NO="0";
    @Autowired
    @Qualifier(value = "userServiceImpl")
    UserService userService;
    @Autowired
    @Qualifier(value = "activityServiceImpl")
    ActivityService activityService;
    public UserService getUserService() {
        return userService;
    }
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    public ActivityService getActivityService() {
        return activityService;
    }
    public void setActivityService(ActivityService activityService) {
        this.activityService = activityService;
    }

    /*处理模态窗口-创建-所有者-下拉列表获得信息*/
    @RequestMapping(value="/owner.do")
    @ResponseBody
    public List<User> getUserList(){
        List<User> list=userService.activityModelGetUserList();
        return list;
    }

    /*处理模态窗口-创建-保存*/
    @RequestMapping(value="/activityCreate.do",produces = "text/plain;charset=utf-8",method = RequestMethod.POST)
    @ResponseBody
    public String preserve(Activity activity, HttpServletRequest request) throws ActivityCreateException {
        String id=UUIDUtil.getUUID();
        String time= DateTimeUtil.getSysTime();
        String createBy=((User)request.getSession().getAttribute("user")).getName();
        activity.setId(id);
        activity.setCreateTime(time);
        activity.setCreateBy(createBy);
        int sum=activityService.activityNewPreserve(activity);//如果创建失败会抛出异常
        //{"yesOrNo":"yes"}
        return "{\"yesOrNo\":\"yes\"}";
    }

    /*处理市场活动中的列表显示内容*/
    @RequestMapping(value="/activityShowList.do",method = RequestMethod.GET)
    @ResponseBody
    public PaginationVo<Activity> showList(String pageNo,String pageSize,String searchName,String searchOwner,String searchStartDate,String searchEndDate){
        int pn=Integer.valueOf(pageNo);
        int ps=Integer.valueOf(pageSize);
        int start=(pn-1)*ps;
        PaginationVo<Activity> paginationVo =activityService.getTotalAndList(start,ps,searchName,searchOwner,searchStartDate,searchEndDate);
        //int a=paginationVo.getTotal();
        return paginationVo;
    }
    //删除
    @RequestMapping(value="/activityDelete.do",method = RequestMethod.POST)
    @ResponseBody
    public String delete(String []id) throws ActivityDeleteException {
        int num=activityService.activityDelete(id);
      /*  for(String s:id){
            System.out.println("hhhh:"+s);
        }*/

        return "1";
    }
    //修改-模态窗口加载
    @RequestMapping(value = "/activityUpdate.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateActivity(String id){
        Map<String,Object> map=activityService.activityUpdate(id);
        return map;
    }

    //修改-更新按钮
    @RequestMapping(value="/activityUpdatePoint.do",method=RequestMethod.POST)
    @ResponseBody
    public String updateActivityPoint(Activity activity,HttpServletRequest request) throws ActivityUpdateException {
        activity.setEditTime(DateTimeUtil.getSysTime());//修改要有修改时间
        String updaterName=((User)request.getSession().getAttribute("user")).getName();//修改要有修改人
        activity.setEditBy(updaterName);
        activityService.activityUpdatePoint(activity);
        return "{\"yesOrNo\":\"yes\"}";
    }

    //市场活动详细页
    @RequestMapping(value="/detail.do",method=RequestMethod.GET)
    public ModelAndView detailInf(String id){
        Map<String,Object> map=activityService.getDetailInfList(id);

        ModelAndView mv=new ModelAndView();
        mv.addObject("activity",map.get("activity"));
        //mv.addObject();
        mv.setViewName("/workbench/activity/detail.jsp");
        return mv;
    }

    //市场活动详细页-备注
    @RequestMapping(value="/activityDetail.do")
    @ResponseBody
    public List<ActivityRemark> detailListRemark(String activityId){
        List<ActivityRemark> list=activityService.activityListRemark(activityId);
        return list;
    }

    //市场活动详细页-备注删除
    @RequestMapping(value="/activityRemarkDel.do",method = RequestMethod.POST)
    @ResponseBody
    public String deleteRemark(String id) throws ActivityRemarkException {
        activityService.remarkDelete(id);
        return "{\"yesOrNo\":\"yes\"}";
    }

    @RequestMapping(value = "/activityRemarkAdd.do",method=RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addActivityRemark(String id,String remark,HttpServletRequest request){
        String idd=UUIDUtil.getUUID();
        String time=DateTimeUtil.getSysTime();
        String createBy=((User)request.getSession().getAttribute("user")).getName();
        ActivityRemark activityRemark=new ActivityRemark();
        activityRemark.setId(idd);
        activityRemark.setNoteContent(remark);
        activityRemark.setCreateBy(createBy);
        activityRemark.setCreateTime(time);
        activityRemark.setEditFlag(EDIT_FLAG_NO);
        activityRemark.setActivityId(id);
        activityService.remarkAdd(activityRemark);
        //{"yesOrNo":"yes","ar":activity} ,\"ar\":"+activityRemark+"
        Map<String,Object> map=new HashMap<>();
        map.put("yesOrNo","yes");
        map.put("ar",activityRemark);
        return map;
    }

    @RequestMapping(value="/activityRemarkUpd.do",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateActivityRemark(String id,String noteContent,HttpServletRequest request){
        ActivityRemark activityRemark=new ActivityRemark();
        activityRemark.setId(id);
        activityRemark.setNoteContent(noteContent);
        activityRemark.setEditBy(((User)request.getSession().getAttribute("user")).getName());
        activityRemark.setEditTime(DateTimeUtil.getSysTime());
        activityRemark.setEditFlag(EDIT_FLAG_YES);
        //activityService.remarkUpdate(activityRemark);
        Map<String,Object> map=new HashMap<>();
        map.put("yesOrNo","yes");
        map.put("ar",activityRemark);
        return map;
    }
}
